import Vue from 'vue'
import VueRouter from 'vue-router'
// import Films from '@/views/Films.vue'
// import Nowplaying from '@/views/films/Nowplaying.vue'
// import Comingsoon from '@/views/films/Comingsoon.vue'
// import Detail from '@/views/Detail.vue'
// import Cinemas from '@/views/Cinemas.vue'
// import Search from '@/views/Search.vue'
// import Center from '@/views/Center.vue'
// import Login from '@/views/Login.vue'

import LoginRouter from '@/views/login/route.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '*', // 优先级最低，其他的路径都匹配不上就会匹配这个，和放置的顺序无关
    redirect: '/films' // 重定向
  },
  // 当项目打包时,javascript包会变得很大,影响页面加载;因此可以进行路由懒加载
  // 路由懒加载 当浏览此页面时才会加载
  // 利用此将组件按组分块
  //  /* webpackChunkName:"group" */  将需要的组件打包在同个异步块中(自己命名)
  {
    name: 'films',
    path: '/films',
    component: () => import(/* webpackChunkName:"films" */ '@/views/Films.vue'), // 路由懒加载
    children: [
      {
        name: 'nowplaying',
        path: '/films/nowplaying',
        component: () => import(/* webpackChunkName:"films" */ '@/views/films/Nowplaying.vue') // 路由懒加载
      },
      {
        name: 'comingsoon',
        path: '/films/comingsoon',
        component: () => import(/* webpackChunkName:"comingsoon" */ '@/views/films/Comingsoon.vue') // 路由懒加载
      },
      {
        path: '/films',
        redirect: '/films/nowplaying' // 重定向
      }
    ]
  },
  {
    name: 'detail',
    path: '/detail/:filmId', // 动态二级路由，二级路径不确定
    component: () => import(/* webpackChunkName:"detail" */ '@/views/Detail.vue') // 路由懒加载
  },
  {
    name: 'city',
    path: '/city',
    component: () => import(/* webpackChunkName:"films" */ '@/views/City.vue') // 路由懒加载
  },
  {
    name: 'cinemas',
    path: '/cinemas',
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/Cinemas.vue'), // 路由懒加载
    meta: {
      isRequired: true
    }
  },
  {
    name: 'cinemafilm',
    path: '/cinema/:cinemaId/film/:filmId?/:timeStamp?', // ? 表示该参数非必传
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/CinemaFilm.vue') // 路由懒加载
  },
  {
    name: 'filmscinemas',
    path: '/film/:filmId/cinemas',
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/FilmCinemas.vue'), // 路由懒加载
    meta: {
      isRequired: true
    }
  },
  {
    name: 'search',
    path: '/cinemas/search',
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/Search.vue') // 路由懒加载
  },
  {
    name: 'schedule',
    path: '/schedule/:scheduleId/:timeStamp',
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/Schedule.vue') // 路由懒加载
  },
  {
    name: 'alipay',
    path: '/alipay',
    component: () => import(/* webpackChunkName:"cinemas" */ '@/views/alipay.vue') // 路由懒加载
  },
  {
    name: 'news',
    path: '/news',
    component: () => import(/* webpackChunkName:"news" */ '@/views/News.vue') // 路由懒加载
  },
  {
    name: 'center',
    path: '/center',
    component: () => import(/* webpackChunkName:"center" */ '@/views/Center.vue') // 路由懒加载
  },
  ...LoginRouter
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 解决bug：Redirected when going from "/films/comingsoon" to "/detail/5667" via a navigation guard.
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  // if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// 去掉路由重复报错的问题

// 全局拦截(守卫)
// router.beforeEach((to, from, next) => {
//   if (to.meta.isRequired) {
//     if (localStorage.getItem('token')) {
//       next()
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
